/**
 * 
 */
package edu.mobbuzz.web.pages;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.Retain;
import org.apache.tapestry5.beaneditor.BeanModel;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.BeanModelSource;

import edu.mobbuzz.daf.bean.Stream;
import edu.mobbuzz.daf.bean.User;
import edu.mobbuzz.web.base.SecureBasePage;
import edu.mobbuzz.web.facade.TrafficFacade;

/**
 * @author penyihirkecil
 *
 */
public class TrafficGridPage extends SecureBasePage{
	private TrafficFacade trafficFacade;
	
	private List<Stream> streamList;
	@Property
	private Stream stream;
	
	@Retain
	private BeanModel streamModel;
	@Inject
	private BeanModelSource beanModelSource;
	@Inject
	private ComponentResources resources;
	@Component
	private Form streamForm;
	
	
	private boolean delete;
	private List<String> deletedList;
	
	public boolean isDelete() {
		return delete;
	}
	
	public void setDelete(boolean delete) {
		if(delete){
			getDeletedList().add(stream.getId());
		}
		this.delete = delete;
	}	
	
	public List<String> getDeletedList() {
		if(deletedList == null){
			deletedList = new ArrayList<String>();
		}
		return deletedList;
	}

	public TrafficFacade getTrafficFacade() {
		if(trafficFacade==null)
			trafficFacade = new TrafficFacade();
		return trafficFacade;
	}

	public BeanModel getStreamModel() {
		return streamModel;
	}	
	
	public List<Stream> getStreamList() {
		if(streamList==null){
			streamList = new ArrayList<Stream>();
		}
		streamList = getTrafficFacade().getTrafficList("","");		
		return streamList;
	}

	public void setStreamList(List<Stream> streamList) {
		this.streamList = streamList;
	}
	
	void pageLoaded(){
		streamModel = beanModelSource.createDisplayModel(Stream.class, resources.getMessages());
		streamModel.add("actions",null).label("actions");
			
	}
	
	Object onSuccessFromStreamForm(){		
		for(String element : getDeletedList()){
			stream = (Stream)getTrafficFacade().getTrafficByPrimaryKey(element);	
			if(stream!=null)
				getTrafficFacade().removeTraffic(stream);
		}		
		return TrafficGridPage.class;
	}	
}
